home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / QFLOAT / QFLOOR.C < prev    next >
C/C++ Source or Header  |  1996-02-25  |  2KB  |  148 lines

  1.  
  2. /* qfloor - largest integer not greater than x
  3.  * qround - nearest integer to x
  4.  */
  5.  
  6. /* #include "mconf.h"  */
  7. #include "qhead.h"
  8.  
  9. extern QELT qone[], qhalf[];
  10.  
  11. #if WORDSIZE == 32
  12. static QELT bmask[] = {
  13. 0xffffffff,
  14. 0xfffffffe,
  15. 0xfffffffc,
  16. 0xfffffff8,
  17. 0xfffffff0,
  18. 0xffffffe0,
  19. 0xffffffc0,
  20. 0xffffff80,
  21. 0xffffff00,
  22. 0xfffffe00,
  23. 0xfffffc00,
  24. 0xfffff800,
  25. 0xfffff000,
  26. 0xffffe000,
  27. 0xffffc000,
  28. 0xffff8000,
  29. 0xffff0000,
  30. 0xfffe0000,
  31. 0xfffc0000,
  32. 0xfff80000,
  33. 0xfff00000,
  34. 0xffe00000,
  35. 0xffc00000,
  36. 0xff800000,
  37. 0xff000000,
  38. 0xfe000000,
  39. 0xfc000000,
  40. 0xf8000000,
  41. 0xf0000000,
  42. 0xe0000000,
  43. 0xc0000000,
  44. 0x80000000,
  45. 0x00000000
  46. };
  47. #else
  48. static short bmask[] = {
  49. 0xffff,
  50. 0xfffe,
  51. 0xfffc,
  52. 0xfff8,
  53. 0xfff0,
  54. 0xffe0,
  55. 0xffc0,
  56. 0xff80,
  57. 0xff00,
  58. 0xfe00,
  59. 0xfc00,
  60. 0xf800,
  61. 0xf000,
  62. 0xe000,
  63. 0xc000,
  64. 0x8000,
  65. 0x0000,
  66. };
  67. #endif
  68.  
  69. int qfloor( x, y )
  70. QELT x[], y[];
  71. {
  72. QELT t[NQ];
  73. register QELT *p;
  74. int e;
  75.  
  76. qmov( x, t );
  77. if( t[1] == 0 )
  78.     {
  79.     qclear( y );
  80.     return(0);
  81.     }
  82. e = t[1] - (EXPONE-1);
  83.  
  84. if( e <= 0 )
  85.     {
  86.     if( t[0] != 0 )
  87.         {
  88.         qmov( qone, y );
  89.         qneg( y );
  90.         }
  91.     else
  92.         {
  93.         qclear( y );
  94.         }
  95.     return(0);
  96.     }
  97.  
  98. /* number of bits to clear out */
  99. e = NBITS - e;
  100.  
  101. qmov( t, y );
  102. p = (QELT *)&y[NQ-1];
  103.  
  104. while( e >= WORDSIZE )
  105.     {
  106.     *p-- = 0;
  107.     e -= WORDSIZE;
  108.     }
  109.  
  110. /* clear the remaining bits */
  111. *p &= bmask[e];
  112.  
  113. /* truncate negatives toward minus infinity */
  114. if( t[0] != 0 )
  115.     {
  116.     if( qcmp( t, y ) != 0 )
  117.         qsub( qone, y, y );
  118.     }
  119. return(0);
  120. }
  121.  
  122. int qround(x, y)
  123. QELT *x, *y;
  124. {
  125. QELT z[NQ], f[NQ];
  126. int r;
  127.  
  128. qfloor( x, z );
  129. qsub( z, x, f );
  130. r = qcmp( f, qhalf );
  131. if( r > 0 )
  132.     goto rndup;
  133. if( r == 0 )
  134.     {
  135. /* round to even */
  136.     z[1] -= 1;
  137.     qfloor( z, f );
  138.     z[1] += 1;
  139.     f[1] += 1;
  140.     if( qcmp(z,f) != 0 )
  141.         {
  142. rndup:        qadd( qone, z, z );
  143.         }
  144.     }
  145. qmov( z, y );
  146. return(0);
  147. }
  148.